package com.arpa.oms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.arpa.core.utils.collection.CollectionUtil;
import com.arpa.oms.domain.enums.OmsGoodTypeEnum;
import com.arpa.oms.service.IOmsGoodsService;
import com.arpa.wms.cache.GoodsCache;
import com.arpa.wms.cache.UnitCache;
import com.arpa.wms.domain.entity.Goods;
import com.arpa.wms.domain.enums.StatusEnum;
import com.arpa.wms.mapper.GoodsMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.arpa.wms.domain.consts.CommonConst.STATUS;

/**
 * @author: Allen chen
 * @Date 2020/8/13
 * @Description:
 */
@Service
public class OmsGoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IOmsGoodsService {
    @Resource
    private GoodsCache goodsCache;
    @Resource
    private UnitCache unitCache;

    /**
     * 模糊查询商品
     *
     * @param keywords     商品名称、编码、条码
     * @param shipmentCode
     */
    @Override
    public List<Map<String, Object>> searchPure(String keywords, String shipmentCode) {
        List<Map<String, Object>> goods = goodsCache.getAllByShipmentCode2(shipmentCode);
        if (IterUtil.isNotEmpty(goods)) {
            // 2020/12/09 jinss  只查询启用状态的商品
            goods = goods.stream().filter(item -> item.get(STATUS).equals(StatusEnum.ACTIVE.getValue())).collect(Collectors.toList());
            if (StrUtil.isNotBlank(keywords)) {
                // 根据关键词过滤商品
                goods = goods.stream().filter(r ->
                        StrUtil.containsIgnoreCase(MapUtil.getStr(r, "name"), keywords)||
                                StrUtil.containsIgnoreCase(MapUtil.getStr(r, "goodCode"), keywords)||
                                StrUtil.containsIgnoreCase(MapUtil.getStr(r, "barCode"), keywords)
                ).collect(Collectors.toList());
                goods.forEach(g -> g.put("recordType", OmsGoodTypeEnum.GOOD_TYPE_REAL.getCode()));
            }
        }
        return goods;
    }


    /**
     * 根据商品code，拼装商品信息
     *
     * @param code
     * @param recordType
     * @return
     */
    @Override
    public Map<String, Object> makeGoods(String code, Integer recordType) {
        if (OmsGoodTypeEnum.GOOD_TYPE_COMPOSE.getCode().equals(recordType)) {
            List<Goods> list = baseMapper.selectList(new QueryWrapper<Goods>().lambda().eq(Goods::getCode, code));
            if (CollectionUtil.isNotEmpty(list)) {
                Goods goods = list.get(0);
                goods.setBasicUnitName(unitCache.translate(goods.getUnitCode()));
                return BeanUtil.beanToMap(list.get(0));
            }
        }
        return goodsCache.get(code);
    }

    /**
     * 模糊查询商品
     *
     * @param goodName     商品名称
     * @param goodCode     商品编码
     * @param goodBarCode     商品条码
     * @param shipmentCode 入驻商标识
     */
    @Override
    public List<Map<String, Object>> searchGoods(String goodName, String goodCode,String goodBarCode, String shipmentCode){
        List<Map<String, Object>> goods = goodsCache.getAllByShipmentCode2(shipmentCode);
        if (IterUtil.isNotEmpty(goods)) {
            // 2020/12/09 jinss  只查询启用状态的商品
            goods = goods.stream().filter(item -> item.get(STATUS).equals(StatusEnum.ACTIVE.getValue()) && item.get("purchase").equals(1)).collect(Collectors.toList());
            if (StrUtil.isNotBlank(goodName) || StrUtil.isNotBlank(goodCode) || StrUtil.isNotBlank(goodBarCode)) {
                // 根据关键词过滤商品
                goods = goods.stream().filter(r ->
                        StrUtil.containsIgnoreCase(MapUtil.getStr(r, "name"), goodName)&&
                                StrUtil.containsIgnoreCase(MapUtil.getStr(r, "goodCode"), goodCode)&&
                                StrUtil.containsIgnoreCase(MapUtil.getStr(r, "barCode"), goodBarCode)
                ).collect(Collectors.toList());
                goods.forEach(g -> g.put("recordType", OmsGoodTypeEnum.GOOD_TYPE_REAL.getCode()));
            }
        }
        return goods;
    }
}
